﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HotDealD2.Models.Repository;
using HotDealD2.Models.ModelView;
using System.Web.Security;

namespace HotDealD2.Models.ModelManager
{
    public class AccountModel
    {
        private QLDataBaseDataContext dataContext;
        public AccountModel()
        {
            dataContext = new QLDataBaseDataContext();
        }

        public IList<ThanhVien> GetListAllAccount()
        {
            
            var listThanhVien = from list in dataContext.ThanhViens select list;
            return listThanhVien.ToList();
        }
        public IList<ThanhVien> GetListAllMemberAccount()
        {

            var listThanhVien = from list in dataContext.ThanhViens where list.MaLoaiThanhVien==3 select list;
            return listThanhVien.ToList();
        }
        public IList<ThanhVien> GetListAllBusinessAccount()
        {

            var listThanhVien = from list in dataContext.ThanhViens where list.MaLoaiThanhVien == 2 select list;
            return listThanhVien.ToList();
        }
        public IList<NguoiDung> GetListAllUser()
        {
            var listNguoiDung = from list in dataContext.NguoiDungs select list;
            return listNguoiDung.ToList();
        }
        
        public bool InsertAccount(RegisterView regisView)
        {
            try
            {
                IList<ThanhVien> listthanhvien = GetListAllAccount();
                NguoiDung ngdung = new NguoiDung();
                //  ngdung.MaNguoiDung = listthanhvien.Count;
                ngdung.Email = regisView.Email;
                ngdung.SoDienThoai = regisView.PhoneNumber;
                ngdung.HoTen = regisView.Fullname;
                // ngdung.NgaySinh = regisView.Birthday;
                ngdung.TinhThanh = regisView.SelectedCity;
                ngdung.QuanHuyen = regisView.SelectedDistrict;
                ngdung.Duong = regisView.Street;
                ngdung.SoNha = regisView.HouseNumber;
                ngdung.PhuongXa = regisView.Town;
                dataContext.NguoiDungs.InsertOnSubmit(ngdung);
                dataContext.SubmitChanges();
                ThanhVien thanhvien = new ThanhVien();
                thanhvien.Username = regisView.UserName;
                thanhvien.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(regisView.Password.Trim(), "md5");
                //thanhvien.MaNguoiDung = listthanhvien.Count;
                thanhvien.MaLoaiThanhVien = 3;
                thanhvien.MaNguoiDung = ngdung.MaNguoiDung;
                
                
                dataContext.ThanhViens.InsertOnSubmit(thanhvien);

                dataContext.SubmitChanges();
                return true;
            }
            catch {
                return false;
            }
        }
        public bool UpdateAccount(UpdateAccountView updateAccount)
        {
            try
            {
                ThanhVien tv = dataContext.ThanhViens.Single(ltv => ltv.Username == updateAccount.UserName);
                if (tv == null)
                {
                    return false;
                }                
                tv.NguoiDung.NgaySinh = updateAccount.Birthday;
                tv.NguoiDung.HoTen = updateAccount.Fullname;
                tv.NguoiDung.GioiTinh = updateAccount.GioiTinh;
                tv.NguoiDung.TinhThanh = updateAccount.SelectedCity;
                tv.NguoiDung.QuanHuyen = updateAccount.SelectedDistrict;
                tv.NguoiDung.PhuongXa = updateAccount.Town;
                tv.NguoiDung.Duong = updateAccount.Street;
                tv.NguoiDung.SoNha = updateAccount.HouseNumber;
                tv.NguoiDung.SoDienThoai = updateAccount.PhoneNumber;
                dataContext.SubmitChanges();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }
        public bool InsertBusiness(RegisterView regisView)
        {

            try
            {
                IList<ThanhVien> listthanhvien = GetListAllAccount();

                
                NguoiDung ngdung = new NguoiDung();
                ngdung.MaNguoiDung = listthanhvien.Count;
                ngdung.Email = regisView.Email;
                ngdung.SoDienThoai = regisView.PhoneNumber;
                ngdung.HoTen = regisView.Fullname;
                // ngdung.NgaySinh = regisView.Birthday;
               // ngdung.TinhThanh = GetTinhThanh(regisView.SelectedCity).TenTinhThanh;
               // ngdung.QuanHuyen = GetQuanHuyen(regisView.SelectedDistrict,regisView.SelectedCity).TenQuanHuyen;
                ngdung.Duong = regisView.Street;
                ngdung.SoNha = regisView.HouseNumber;
                ngdung.PhuongXa = regisView.Town;
                dataContext.NguoiDungs.InsertOnSubmit(ngdung);
                dataContext.SubmitChanges();
                ThanhVien thanhvien = new ThanhVien();
                thanhvien.Username = regisView.UserName;
                thanhvien.Password = regisView.Password;
                thanhvien.MaNguoiDung = listthanhvien.Count;
                thanhvien.MaLoaiThanhVien = 2;
                thanhvien.MaNguoiDung = ngdung.MaNguoiDung;
                

                dataContext.ThanhViens.InsertOnSubmit(thanhvien);

                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool UpdateAccount(ThanhVien thanhVien)
        {
            try
            {
                ThanhVien temp = dataContext.ThanhViens.Single(p => p.Username == thanhVien.Username);
                temp.NguoiDung.HoTen = thanhVien.NguoiDung.HoTen;
                temp.NguoiDung.Duong = thanhVien.NguoiDung.Duong;
                temp.NguoiDung.PhuongXa = thanhVien.NguoiDung.PhuongXa;
                temp.NguoiDung.SoNha = thanhVien.NguoiDung.SoNha;
                temp.NguoiDung.SoDienThoai = thanhVien.NguoiDung.SoDienThoai;
                temp.NguoiDung.QuanHuyen = thanhVien.NguoiDung.QuanHuyen;
                temp.NguoiDung.TinhThanh = thanhVien.NguoiDung.TinhThanh;
                if(thanhVien.DangKyDoanhNghiep ==true)
                temp.DangKyDoanhNghiep = thanhVien.DangKyDoanhNghiep;
                
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool UpdateAccountToBusiness(ThanhVien thanhVien)
        {
            try
            {
                ThanhVien temp = dataContext.ThanhViens.Single(p => p.Username == thanhVien.Username);
                temp.NguoiDung.HoTen = thanhVien.NguoiDung.HoTen;
                temp.NguoiDung.Duong = thanhVien.NguoiDung.Duong;
                temp.NguoiDung.PhuongXa = thanhVien.NguoiDung.PhuongXa;
                temp.NguoiDung.SoNha = thanhVien.NguoiDung.SoNha;
                temp.NguoiDung.SoDienThoai = thanhVien.NguoiDung.SoDienThoai;
                temp.NguoiDung.QuanHuyen = thanhVien.NguoiDung.QuanHuyen;
                temp.NguoiDung.TinhThanh = thanhVien.NguoiDung.TinhThanh;
                
                    temp.DangKyDoanhNghiep = false;
                temp.MaLoaiThanhVien = 2;
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool UpdatePassWord(string username,string newpassword)
        {
            try
            {
                ThanhVien temp = dataContext.ThanhViens.Single(p => p.Username == username);
                temp.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(newpassword.Trim(), "md5");
             
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteAccount(string userName)
        {
            try
            {
                var thanhvien = (from tv in dataContext.ThanhViens
                                      where tv.Username == userName
                                      select tv).First();
                IList<Comment> listcomment = (from comment in dataContext.Comments
                                              where comment.Username == thanhvien.Username
                                              select comment).ToList();


                IList<DonHang> listdonhang = (from donhang in dataContext.DonHangs
                                              where donhang.UsernameMember == thanhvien.Username
                                              select donhang).ToList();
                var id = thanhvien.MaNguoiDung;
                for (int i = 0; i < listdonhang.Count; i++)
                {
                    List<DanhSachSanPham> listorder = (from order in dataContext.DanhSachSanPhams
                                                       where order.MaDonHang == listdonhang[i].MaDonHang
                                                       select order).ToList();

                    dataContext.DanhSachSanPhams.DeleteAllOnSubmit(listorder);
                }
                dataContext.Comments.DeleteAllOnSubmit(listcomment);
                dataContext.DonHangs.DeleteAllOnSubmit(listdonhang);
                dataContext.NguoiDungs.DeleteOnSubmit(thanhvien.NguoiDung);
              
          
                dataContext.ThanhViens.DeleteOnSubmit(thanhvien);
             
                dataContext.SubmitChanges();

            /*   IList<ThanhVien> listthanhvien = GetListAllAccount();
                IList<NguoiDung> listnguoidung = GetListAllUser();
                for (int i = id; i < listthanhvien.Count; i++)
                {

                    listnguoidung[i].MaNguoiDung -= 1;
                    dataContext.NguoiDungs.Attach(listnguoidung[i], true);
                    dataContext.SubmitChanges();
                    
                 
                }
                dataContext.SubmitChanges();*/
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteBusiness(string userName)
        {
            try
            {
                var thanhvien = (from tv in dataContext.ThanhViens
                                 where tv.Username == userName
                                 select tv).First();
                IList<Comment> listcomment = (from comment in dataContext.Comments
                                              where comment.Username == thanhvien.Username
                                              select comment).ToList();

                IList<DealChuaDuyet> listdealchuaduyet= (from deal in dataContext.DealChuaDuyets
                                              where deal.UsernameDoanhNghiep == thanhvien.Username
                                              select deal).ToList();

                IList<DealDaDuyet> listdealdaduyet = (from deal in dataContext.DealDaDuyets
                                                          where deal.UsernameDoanhNghiep == thanhvien.Username
                                                          select deal).ToList();

                IList<Deal> listdeal = (from deal in dataContext.Deals
                                                          where deal.UsernameDoanhNghiep == thanhvien.Username
                                                          select deal).ToList();

                dataContext.DealChuaDuyets.DeleteAllOnSubmit(listdealchuaduyet); 
                dataContext.DealDaDuyets.DeleteAllOnSubmit(listdealdaduyet);
                dataContext.Deals.DeleteAllOnSubmit(listdeal);
                dataContext.Comments.DeleteAllOnSubmit(listcomment);
                dataContext.NguoiDungs.DeleteOnSubmit(thanhvien.NguoiDung);


                dataContext.ThanhViens.DeleteOnSubmit(thanhvien);

                dataContext.SubmitChanges();

 
                return true;
            }
            catch
            {
                return false;
            }
        }
        public ThanhVien GetAccount(string userName)
        {
            return dataContext.ThanhViens.Single(tv => tv.Username == userName);
        }
        public ThanhVien GetAccountFromID(int id)
        {
            return dataContext.ThanhViens.Single(tv => tv.MaNguoiDung == id);
        }
        public UpdateAccountView GetAccountToUpdate(string userName)
        {
            ThanhVien tv = dataContext.ThanhViens.Single(tvs => tvs.Username == userName);
            UpdateAccountView regAccount = new UpdateAccountView();
            regAccount.UserName = tv.Username;
            regAccount.Email = tv.NguoiDung.Email;
            regAccount.Password = tv.Password;
            regAccount.Birthday = tv.NguoiDung.NgaySinh;
            regAccount.Fullname = tv.NguoiDung.HoTen;
            regAccount.SelectedCity = tv.NguoiDung.TinhThanh;
            regAccount.SelectedDistrict = tv.NguoiDung.QuanHuyen;
            regAccount.HouseNumber = tv.NguoiDung.SoNha;
            regAccount.Street = tv.NguoiDung.Duong;
            regAccount.Town = tv.NguoiDung.PhuongXa;
            regAccount.PhoneNumber = tv.NguoiDung.SoDienThoai;

            return regAccount;
        }
        public TinhThanh GetTinhThanh(int maTinhThanh)
        {
            return dataContext.TinhThanhs.Single(tv => tv.MaTinhThanh == maTinhThanh);
        }
        public QuanHuyen GetQuanHuyen(int maQuanHuyen,int maTinhThanh)
        {
            return dataContext.QuanHuyens.Single(tv => tv.MaQuanHuyen == maQuanHuyen);
        }
        public IList<TinhThanh> GetAllTinhThanh()
        {
            var listTinhThanh = from list in dataContext.TinhThanhs select list;
            return listTinhThanh.ToList();
        }
        public IList<QuanHuyen> GetAllQuanHuyen(int maTinhThanh)
        {
            var listQuanHuyen = from list in dataContext.QuanHuyens where list.MaTinhThanh ==maTinhThanh select list;
            return listQuanHuyen.ToList();
        }
        public IList<ThanhVien> SearchAccount(string userName)
        {

             var listThanhVien = from o in dataContext.ThanhViens where (o.Username.Contains(userName)) select o;
             return listThanhVien.ToList();
        }
        public bool IsUserLoginIDExist(string userLogIn)
        {
            return (from o in dataContext.ThanhViens where o.Username == userLogIn select o).Any();
        }

        public bool IsEmailAddressExist(string email)
        {
            return (from o in dataContext.NguoiDungs where o.Email == email select o).Any();
        }
        public bool IsEmailAddressGetDealExist(string email)
        {
            return (from o in dataContext.EmailNhanDeals where o.Email == email select o).Any();
        }
        public bool IsPassWordNotRight(string username,string password)
        {
            return (from o in dataContext.ThanhViens where (o.Username == username && o.Password == FormsAuthentication.HashPasswordForStoringInConfigFile(password.Trim(), "md5")) select o).Any();
        }
        public bool CheckLogon(string userLogon,string passWord)
        {
            return (from o in dataContext.ThanhViens where (o.Username == userLogon && o.Password== FormsAuthentication.HashPasswordForStoringInConfigFile(passWord.Trim(), "md5"))  select o).Any();
        }
    }
}